package com.clf.common.util.remote;

import com.clf.common.util.enums.AppStatusEnum;
import com.clf.common.util.enums.BizResultCodeEnum;
import com.clf.common.util.page.PageQuery;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 推荐只在前端调用Controller的接口层使用, 即对外提供服务的最外层.<br>
 *
 * @param <V>
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class HttpResult<V> implements Serializable {

    /**
     * 序列化接口
     */
    private static final long serialVersionUID = 830949326026790112L;

    /**
     * 接口返回状态
     * 0|成功,1|前台参数错误, 2|后台程序异常, 3|获取数据为空或者非预期数据,4|token失效, 5|其他
     */
    private Integer status;

    /**
     * 接口返回数据
     */
    private V result = null;

    /**
     * 异常时，返回的错误描述字符串
     */
    private String msg = null;

    /**
     * 构建httpResult
     *
     * @param status  返回结果状态
     * @param result  返回结果
     * @param message 返回提示信息
     * @return
     */
    private static HttpResult<Object> createHttpResult(Integer status, Object result, String message) {
        HttpResult<Object> builder = new HttpResult<Object>();
        builder.setMsg(message);
        builder.setStatus(status);
        if (result == null) {
            result = new HashMap<>();
        }
        builder.setResult(result);
        return builder;
    }

    public static HttpResult<Object> createHttpResult(Integer status, String message) {
        HttpResult<Object> builder = new HttpResult<Object>();
        builder.setMsg(message);
        builder.setStatus(status);
        builder.setResult(new HashMap<>(0));
        return builder;
    }

    /**
     * 创建token失效结果
     *
     * @return
     */
    public static HttpResult<Object> createInvalidTokenResult() {
        return createHttpResult(AppStatusEnum.INVALID_TOKEN.getCode(), null, AppStatusEnum.INVALID_TOKEN.getValue());
    }

    /**
     * 创建会话失效结果
     *
     * @return
     */
    public static HttpResult<Object> createSessionInvalidResult() {
        return createHttpResult(AppStatusEnum.SESSION_INVALID.getCode(), null, AppStatusEnum.SESSION_INVALID.getValue());
    }

    /**
     * 创建会话失效结果
     *
     * @return
     */
    public static HttpResult<Object> createSessionInvalidResult(String message) {
        return createHttpResult(AppStatusEnum.SESSION_INVALID.getCode(), null, message);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult() {
        return createParamErrorResult((Object) null);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult(String message) {
        return createHttpResult(AppStatusEnum.PARAM_ERROR.getCode(), null, message);
    }

    /**
     * 创建请求参数错误结果
     *
     * @return
     */
    public static HttpResult<Object> createParamErrorResult(Object result) {
        return createHttpResult(AppStatusEnum.PARAM_ERROR.getCode(), result, AppStatusEnum.PARAM_ERROR.getValue());
    }

    /**
     * 创建解密失败返回信息
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createDecryptErrorResult() {
        return createHttpResult(AppStatusEnum.DECRYPT_ERROR.getCode(), null, AppStatusEnum.DECRYPT_ERROR.getValue());
    }

    /**
     * 创建解密失败返回信息
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createDecryptErrorResult(Object result) {
        return createHttpResult(AppStatusEnum.DECRYPT_ERROR.getCode(), result, AppStatusEnum.DECRYPT_ERROR.getValue());
    }

    /**
     * 返回文本提示结果
     *
     * @return
     */
    public static HttpResult<Object> createMessageResult() {
        return createMessageResult((Object) null);
    }

    /**
     * 返回文本提示结果
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createMessageResult(String message) {
        return createMessageResult(null, message);
    }

    /**
     * 返回文本提示结果
     *
     * @param result
     * @return
     */
    public static HttpResult<Object> createMessageResult(Object result) {
        return createMessageResult(result, AppStatusEnum.NOT_EXPECT_DATA.getValue());
    }

    /**
     * 返回文本提示和结果对象
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createMessageResult(Object result, String message) {
        return createHttpResult(AppStatusEnum.NOT_EXPECT_DATA.getCode(), result, message);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @return
     */
    public static HttpResult<Object> createSuccessResult() {
        return createSuccessResult((Object) null);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param message
     * @return
     */
    public static HttpResult<Object> createSuccessResult(String message) {
        return createSuccessResult(null, message);
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param result
     * @return
     */
    public static HttpResult<Object> createSuccessResult(Object result) {
        return createSuccessResult(result, AppStatusEnum.SUCCESS.getValue());
    }

    /**
     * 业务处理成功后，通过这个方法包装返回对象
     *
     * @param result
     * @param message
     * @return
     */
    public static HttpResult<Object> createSuccessResult(Object result, String message) {
        return createHttpResult(AppStatusEnum.SUCCESS.getCode(), result, message);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult() {
        return createExceptionErrorResult((Object) null);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult(String message) {
        return createHttpResult(AppStatusEnum.EXCEPTION_ERROR.getCode(), null, message);
    }

    /**
     * 当前网络不稳定
     *
     * @return
     */
    public static HttpResult<Object> createExceptionErrorResult(Object result) {
        return createHttpResult(AppStatusEnum.EXCEPTION_ERROR.getCode(), result, AppStatusEnum.EXCEPTION_ERROR.getValue());
    }

    /**
     * 后台管理系统未登陆异常
     *
     * @return
     */
    public static HttpResult<Object> createPMSNoLoginResult() {
        return createHttpResult(BizResultCodeEnum.REQUIRES_AUTH.getCode(), Boolean.FALSE, BizResultCodeEnum.REQUIRES_AUTH.getMsg());
    }

    public static HttpResult<Object> createPMSNoPermissionResult() {
        return createHttpResult(BizResultCodeEnum.INSUFFICIEN_PERMISSIONS.getCode(), Boolean.FALSE, BizResultCodeEnum.INSUFFICIEN_PERMISSIONS.getMsg());
    }

    /**
     * 后台管理系统成功返回的信息
     *
     * @param result
     * @return
     */
    public static HttpResult<Object> createPMSSuccesResult(Object result) {
        return createHttpResult(BizResultCodeEnum.SUCCESS.getCode(), result, BizResultCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 后台管理系统成功返回的信息
     *
     * @return
     */
    public static HttpResult<Object> createPMSSuccesResult() {
        return createPMSSuccesResult(null);
    }

    /**
     * 后台管理系统参数校验错误
     *
     * @param
     * @return
     */
    public static HttpResult<Object> createPMSParameterErrorResult() {
        return createPMSParameterErrorResult(null);
    }

    /**
     * 后台管理系统参数校验错误
     *
     * @param errorMsg
     * @return
     */
    public static HttpResult<Object> createPMSParameterErrorResult(String errorMsg) {
        return createHttpResult(BizResultCodeEnum.PARAMETER_ERROR.getCode(), null, BizResultCodeEnum.PARAMETER_ERROR.getMsg() +
                (StringUtils.isEmpty(errorMsg) ? "" : ":" + errorMsg));
    }

    /**
     * 后台管理系统未检查异常
     *
     * @param
     * @return
     */
    public static HttpResult<Object> createPMSFailureResult() {
        return createPMSFailureResult(null);
    }

    /**
     * 后台管理系统未检查异常
     *
     * @param errorMsg
     * @return
     */
    public static HttpResult<Object> createPMSFailureResult(String errorMsg) {
        return createPMSFailureResult(null, errorMsg);
    }

    /**
     * 后台管理系统未检查异常
     *
     * @param result errorMsg
     * @return
     */
    public static HttpResult<Object> createPMSFailureResult(Object result, String errorMsg) {
        return createHttpResult(BizResultCodeEnum.UN_CHECK_ERROR.getCode(), result, (StringUtils.isEmpty(errorMsg) ? BizResultCodeEnum.UN_CHECK_ERROR.getMsg() : errorMsg));
    }

    /**
     * 后台管理系统成功返回分页
     *
     * @return
     */
    public static HttpResult<Object> createPMSSuccesResultWithPage(Object result, PageQuery query) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", result);
        map.put("pageNo", query.getPageNo());
        map.put("pageSize", query.getPageSize());
        map.put("totalCount", query.getTotalCount());
        return createHttpResult(BizResultCodeEnum.SUCCESS.getCode(), map, BizResultCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 后台管理系统成功返回分页
     *
     * @param data
     * @return
     */
    public static <T extends PageQuery> HttpResult<Object> createPMSSuccesResultWithPage(T data) {
        return createPMSSuccesResultWithPage(data, data);
    }

    public static HttpResult<Object> createPMSFailureResultWithPage(PageQuery query) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", null);
        map.put("pageNo", 1);
        map.put("pageSize", query.getPageSize());
        map.put("totalCount", 0);
        return createPMSFailureResult(map, null);
    }

}
